<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author xuanyan <xuanyan@geek-zoo.com>
 *
 */
require_once ROOT_PATH.'/Abstract/front_abstract.php';

class Action extends front_abstract
{
    function index()
    {
		$hid = _GET('hid', 0);
		$info = array();
		@$cookie_time = array_filter(explode('@', $_COOKIE['time_l']));
		if ($hid) {
			$info = _model('tweet_list')->read(array('id'=>$hid));
		}
	    $this->smarty->assign('time_l', $cookie_time);
	    $this->smarty->assign('info', $info);
	    $this->smarty->display('lib/update.html');
    }

	function save()
	{
		$uids = array_filter(explode(',' ,_GET('uids')));
		$info = _POST('info', array());
		$text = $info['text'];
		$hid = intval($info['hid']);
		if (@$info['img']) {
	       $pic = Config::get('att_url').$info['img'];
	    } else {
	    	$pic = '';
	    }
		
	
		
		@$time_l = array_filter(explode('@', $_COOKIE['time_l']));
		if ($info['time_i'] < 10) {
			$time_ii = '0'.$info['time_i'];
		} else {
			$time_ii = $info['time_i'];
		}
		array_unshift($time_l, $info['time_h'].':'.$time_ii);
		while (count($time_l) > 6) {
			array_pop($time_l);
		}
		$s_cookie = implode('@', $time_l);
		Cookie::set('time_l', $s_cookie, time()+24*3600*365);
		
		$send_time = $info['year'].'-'.$info['month'].'-'.$info['day'].' '.$info['time_h'].':'.$info['time_i'];
		$send_time = strtotime($send_time);
		
		$accounts_ids = array();
		$accounts_info = array();
		
		foreach ($this->accounts as  $value) {
			$accounts_ids[] = $value['id'];
			$accounts_info[$value['id']]['oauth_token'] = $value['oauth_token'];
			$accounts_info[$value['id']]['oauth_token_secret'] = $value['oauth_token_secret'];
		}
		
		if ($hid) { //如果是更新的话
			$h_info = _model('tweet_list')->read(array('id'=>$hid));
			if ($this->weibo_info['id'] != $h_info['account_id']) {
				return '权限不够';
			}
			if ($info['type']) {
				$inert = array('content'=>$text, 'send_time'=>$send_time);
				if ($pic) {
					$inert['pic'] = $pic;
				}
				_model('tweet_list')->update(array('id'=>$hid), $inert);
			} else {
				_model('tweet_list')->delete(array('id'=>$hid));
				if (!$pic && !empty($h_info['img'])) {
					$pic = $h_info['img'];
				}
				
				$api = new ApiSina();
				$api->update($this->oauth_token, $this->oauth_token_secret, $text, $pic);
			}
		} else {
			if ($info['type']) {
				foreach ($uids as $key=>$value) {
					if ($value==$_SESSION['user']['account_id']) {
						_model('tweet_list')->create(array('pic'=>$pic, 'send_time'=>$send_time, 'sent_time'=>0, 'content'=>$text, 'account_id'=>$_SESSION['user']['account_id']));
						unset($uids[$key]);
					}
				}
				foreach ($uids as $val) {
					//检查用户是否属于自己管理范围
					if (in_array($val, $accounts_ids)) {
						$info = _model('account')->read(array('id'=>$val));
						if ($info) {
							_model('tweet_list')->create(array('pic'=>$pic, 'send_time'=>$send_time, 'sent_time'=>0, 'content'=>$text, 'account_id'=>$val));
						}
					}
				}
			} else {
				if ($uids) {
					$api = new ApiSina();
					foreach ($uids as $key=>$value) {
						if ($value==$_SESSION['user']['account_id']) {
							$api->update($this->oauth_token, $this->oauth_token_secret, $text, $pic);
							unset($uids[$key]);
						}
					}
					foreach ($uids as $val) {
						//检查用户是否属于自己管理范围
						if (in_array($val, $accounts_ids)) {
							$info = _model('account')->read(array('id'=>$val));
							if ($info) {
								$api->update($accounts_info[$val]['oauth_token'], $accounts_info[$val]['oauth_token_secret'], $text, $pic);
							}
						}
					}
				}
			}
		}

		
		

	}

}

?>